﻿using Application.AppService.EMRAppService;
using Application.AppService.ERAppService;
using AutoMapper;
using Domain;
using Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

namespace UI.Controllers.RecruitmentManagement_招聘管理_.Post_Publication_Management_职位发布管理_
{
	public class EMRSController : Controller
	{
		private readonly IEMRAppService emrAppService;
		private readonly IMapper mapper;
		private readonly IMemoryCache memCache;
		private readonly ILogger<EMRSController> logger;

		public EMRSController(IEMRAppService emrAppService, IMapper mapper, IMemoryCache memCache, ILogger<EMRSController> logger)
		{
			this.emrAppService = emrAppService;
			this.mapper = mapper;
			this.memCache = memCache;
			this.logger = logger;
		}
		public IActionResult Index()
		{
			return View();
		}
		[HttpPost]
		public async Task<IActionResult> GetEMR()
		{
			List<EMR> emrs = await emrAppService.GetEMR();
			return Json(emrs);
		}
		[HttpPost("DeleteEMR")]
		public async Task<IActionResult> DeleteEMR(int Id)
		{
			int rs = await emrAppService.DeleteEMR(Id);
			return Json(rs);
		}
		[HttpPost("UpdateEMR")]
		public async Task<IActionResult> UpdateEMR(EMRAddDto emrAddDto)
		{
			 EMR emr = mapper.Map<EMR>(emrAddDto);
			int rs = await emrAppService.UpdateEMR(emr);
			return Json(rs);
		}
		[HttpPost("GetChances")]
		public IActionResult GetChances(int page, int pageSize)
		{
			string cacheId1 = "totalRecords";
			string cacheId2 = "chances";
			logger.LogInformation("开始执行Index");
			var totalRecords = memCache.Get(cacheId1);
			var chances = memCache.Get(cacheId2);
			//ERFenYE erFenYE = memCache.Get(cacheId) as ERFenYE;
			if (totalRecords == null&& chances == null)
			{
				logger.LogInformation("数据库查询");
				totalRecords = emrAppService.GetCount();
				chances = emrAppService.GetList(page, pageSize);
				//60秒后失效
				memCache.Set(cacheId1, totalRecords, new TimeSpan(0, 0, 60));
				memCache.Set(cacheId2, chances, new TimeSpan(0, 0, 60));
			}
			else
			{
				logger.LogInformation("从缓存获取");
			}
			//ERFenYE erFenYE = eRAppService.DateERSAsync(page, pageSize, 1);
			//return Json(erFenYE);
			//var totalRecords = emrAppService.GetCount(); // 获取数据总量
			//var chances = emrAppService.GetList(page, pageSize);
			return Json(new { TotalRecords = totalRecords, Chances = chances });
		}
	}
}
